/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package suncertify.ui;

import java.awt.BorderLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author jshah
 * A Runnable class which launches the CSRScreen and displays it till it gets a message to close and exit the screen
 */
   public class CSRScreenDisplay implements Runnable{
        CSRScreen instance ;
        boolean keepRunning = true;
        //flag to show the screen is being displayed first time
        boolean firstTimeDisplay = true;
        /**
         * default constructor gives an instance of <code>CSRScreen</code> to <code>instance</code>
         */
        public CSRScreenDisplay(){
            this.instance = new CSRScreen();
       }
        public void run() {
            
            this.instance.parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.instance.containerPane = this.instance.parentFrame.getContentPane();
            this.instance.containerPane.setLayout(new BorderLayout());
            this.instance.parentPanel = this.instance.getMainPanel();
            this.instance.containerPane.add(this.instance.getMainPanel());
            this.instance.parentFrame.pack();
            this.instance.parentFrame.setVisible(false);
            

            while(this.keepRunning){
                if(ClientTest.showCSR){
                    this.instance.parentFrame.setVisible(true);
                    //force the screen to refresh the first time the table is displayed
                    if(firstTimeDisplay){
                        this.instance.refreshTable.doClick();
                        firstTimeDisplay = false;
                    }
                }
                //anonymous class implementing WindowStateListener Interface to make sure the Thread is terminated when window is closed
                this.instance.parentFrame.addWindowStateListener(new WindowStateListener(){
                    public void windowClosed(WindowEvent e) {
                            //This will only be seen on standard output.
                        Logger.getLogger(CSRScreenDisplay.class.getName()).log(Level.INFO,"CLIENT  CLOSING");
                        //break the loop and terminate the thread
                        CSRScreenDisplay.this.keepRunning = false;
                }

                public void windowStateChanged(WindowEvent e) {

                }
            });
            }

         
        }
    }
